package com.qk.controller;

import com.qk.aop.anno.LogRecord;
import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.domain.customer.CustomerDO;
import com.qk.dto.customer.CustomerDTO;
import com.qk.entity.Customer;
import com.qk.service.CustomerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Author: zhangchuang
 * @Date: 2025/10/16 11:50
 * @Description: 客户控制器
 */
@RestController
@Slf4j
@RequestMapping("/customers")
public class CustomerController {

    @Autowired
    private CustomerService customerService;

    @PutMapping
    public Result updateCustomer(@RequestBody Customer customer) {
        log.info("更新客户信息: {}", customer);
        return customerService.updateCustomer(customer) ? Result.success() : Result.error();
    }

    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        log.info("根据id查询客户信息: {}", id);
        return Result.success(customerService.getById(id));
    }

    @LogRecord
    @PostMapping
    public Result addCustomer(@RequestBody Customer customer) {
        log.info("添加客户信息: {}", customer);
        return customerService.addCustomer(customer) ? Result.success() : Result.error();
    }

    @GetMapping
    public Result listByPage(CustomerDTO dto) {
        log.info("分页查询客户信息: {}", dto);
        PageResult<CustomerDO> result = customerService.getCustomerByPage(dto);
        return Result.success(result);
    }

}
